

/**
 * @method lineX 根据两点求二元一次方程
 * @param {Object} point1 点1{x:"",y:""}
 * @param {Object} point2 点2{x:"",y:""}
 * @return {Object} a为常数，b为系数, 返回x为垂直于x轴的直线，返回Y为垂直于y轴的直线
 * */
function lineX(point1, point2) {
  let x1 = point1.x;
  let x2 = point2.x;
  let y1 = point1.y;
  let y2 = point2.y;
  if (x2 - x1 === 0) {
    return {x: x1};
  }
  //求系数
  let b = (y2 - y1) / (x2 - x1);
  //求常数
  let a = y1 - b * x1;
  return {
    a: a,
    b: b
  }
}

/**
 * @method segmentsIntr  求二元一次方程的交点
 * @param {Object} line1 线条1方程
 * @param {Object} line2 线条2方程
 * @return 交点
 * */
function segmentsIntr(line1, line2) {
  let a1 = line1.a;
  let a2 = line2.a;
  let b1 = line1.b;
  let b2 = line2.b;
  let x, y;
  if (a2 === undefined || b2 === undefined) {
    x = line2.x;
    y = a1 + b1 * x
  } else {
    x = (a1 - a2) / (b2 - b1);
    y = a1 + b1 * x;
  }
  x = parseFloat(parseFloat(x).toFixed(4));
  y = parseFloat(parseFloat(y).toFixed(4));
  return {
    x: x,
    y: y
  }
}

/**
 * @method getDoundary 计算边界
 * @param point
 * @returns {{maxX: Number, minX: Number, maxY: Number, minY: Number}}
 */
function getBoundary(point) {
  let maxX = +point[0].x;
  let minX = +point[0].x;
  let maxY = +point[0].y;
  let minY = +point[0].y;
  for (let i = 0; i < point.length; i++) {
    let pointX = +point[i].x;
    let pointY = +point[i].y;
    if (pointX > maxX) {
      maxX = pointX;
    }
    if (pointX < minX) {
      minX = pointX;
    }
    if (pointY > maxY) {
      maxY = pointY;
    }
    if (pointY < minY) {
      minY = pointY;
    }
  }
  return {
    maxX: parseFloat(parseFloat(maxX).toFixed(4)),
    minX: parseFloat(parseFloat(minX).toFixed(4)),
    maxY: parseFloat(parseFloat(maxY).toFixed(4)),
    minY: parseFloat(parseFloat(minY).toFixed(4))
  }
}

/**
 * @getBlueLineFuncs 获取蓝斜线方法
 * @param {Number/Float} b 斜度
 * @param {Object} config 绘制参数
 * @returns {Array}
 */
function getBlueLineFuncs(b, config) {
  config = config || {};
  b = b || 0.5;
  let blueLineFuncs = [];
  //计算蓝斜线可能显示的最大区域，即根据系数，计算常数的最小值，和最大值
  let svgW = config.width || 15 * 42;
  let svgH = config.height || 15 * 55;

  let disI = -b * 2.5;
  if (b < 0) {
    let a1 = 0;
    let a2 = svgH - b * svgW;
    for (let i = a1; i < a2; i = i + disI) {//
      blueLineFuncs.push({a: i, b: b});
    }
  } else {
    let a1 = -b * svgW;
    let a2 = svgH;
    for (let i = a1; i < a2; i = i - disI) {
      blueLineFuncs.push({a: i, b: b});
    }
  }
  return blueLineFuncs;
}


export {lineX, segmentsIntr, getBoundary, getBlueLineFuncs}
